//TODO: Are we actually using the orders data we got from P&G cos if not we can get rid of the first 3 lines in main and delete dataparser.java?
//TODO: Is it necessary for placeSingleOrder to return a boolean since it's never tested?
//TODO: IS it necessary for updateOrderDeliveries ro return a boolean since it's never tested?
import java.io.*;
import java.util.*;

/**
 * This class holds all the orders that have been submitted to the system.  It
 * also acts as the "main" class for our subsystem.
 */
public class OrdersManager {

    private static List<Order> orders = new ArrayList<Order>();
//        LineItem[] initItems = new LineItem[17];
//        Order initialiser = new Order(0,-1, initItems, new Date());
//        orders.add(initialiser);
    /**
     * This method creates a single order using the relevant information provided.
     *
     * @param userId
     * @param items
     * @param requestedDeliveryDate
     * @param requestedDeliveryTime
     * @return Returns a boolean indicating whether the order was successfully
     * added to the orders list maintained by this class.
     */
    public static boolean placeSingleOrder(int userId, LineItem[] items, Date requestedDeliveryDate) {
        Integer orderId = orders.size();
        Order order = new Order(orderId, userId, items, requestedDeliveryDate);
        System.out.println("Your order id is " + orderId);
        Emailer.createEmail(order);
        Boolean placed = addOrder(order);
        if (placed) {
            DeliveriesDpt.placeOrder(order);
        } else {
            System.err.println("The order was not added to the orders data structure.");
        }
        return placed;
    }

    /**
     * This method is called by deliveries to inform us that they have created
     * estimated times for product deliveries.
     *
     * @param order
     * @return
     */
    public static boolean updateOrderDeliveries(Order order) {
        order.setStatus(Order.ESTIMATE_RECEIVED);
        orders.set((order.getOrderId()), order);
        Emailer.createEmail(order);
        return true;
    }

    /**
     * This method sets the status flag of the order to confirmed, sends it
     * to deliveries for processing, and informs the user via email that their
     * order is in the pipeline.
     *
     * @param orderId The id of the order to confirm
     */
    public static void confirmOrder(int orderId) {
        Order order = getOrderFromId(orderId);
        order.setStatus(Order.ORDER_CONFIRMED);
        DeliveriesDpt.confirmOrder(order.getOrderId());
        Emailer.createEmail(order);
    }

    /**
     * This method first checks to see if this order can be canceled, if it can
     * it sets the status flag of the order to canceled, sends it
     * to deliveries for notification, and informs the user via email that their
     * order has been canceled
     *
     * @param orderId The id of the order to cancel
     */
    public static void cancelOrder(int orderId) {

        Order order = getOrderFromId(orderId);

        if (order.getStatus() != Order.ORDER_SHIPPED) {
            order.setStatus(Order.ORDER_CANCELLED);
            DeliveriesDpt.cancelOrder(orderId);
            Emailer.createEmail(order);
        } else {
            System.out.println("That order has already been shipped and cannot be canceled.");
        }

    }

    public static boolean addOrder(Order order){
        orders.add(order);
        return true;
    }

    /**
     * This method returns the order with the given id, this is trivial since
     * the order id is the index of the order in the orders list
     *
     * @param orderId The id of the order to find
     */
    public static Order getOrderFromId(int orderId) {
        return orders.get(orderId);
    }

    /**
     * @return Returns the list of orders
     */
    public static List<Order> getOrders() {
        return orders;
    }

    /**
     * Place mass orders by uploading a file
     *
     * @param file The file containing the orders
     * @return Returns a boolean indicating whether all orders in the file were
     * successfully added to the orders list maintained by this class
     */
    public static boolean placeMassOrder(File file) {
        // NOT IMPLEMENTED
        return false;
    }

    /**
     * This method returns all orders that a given user has placed
     *
     * @param userId The id of the user
     */
    public static List<Order> getOrdersPlacedByUser(int userId) {
        // NOT IMPLEMENTED
        return null;
    }

    /**
     * This method allows a user to update their order
     *
     * @param userId The id of the user
     * @param orderId The id of the order to update
     * @return Returns true if the order was successfully updated
     */
    public static boolean updateOrderUser(int userId, int orderId) {
        // NOT IMPLEMENTED
        return false;
    }

     /**
     * This method allows a user to split their order
     *
     * @param order The order to be split
     * @return Returns true if the order was successfully updated
     */
    public static Order[] splitOrder(Order order){
        // NOT IMPLEMENTED
        return null;
    }

}
